chrome.browserAction.setBadgeText({text: "wait"});
var Receiver = {
	
	pong: function(data) {
		if(data) {
			var ifr = FMP.jQuery('#ifr');
			
			if(data.token == ifr.data('token')) {
				clearTimeout(ifr.data('timeout'));
				FMP.data.playerLoaded = true;
			}
		}
	},
	
	updatePlay: function(data) {
		FMP.database.query([{query: FMP.squel.update().table('library').set('last_played', "datetime('now', 'localtime')", true).set('times_played', 'times_played + 1', true).where('id', data.id)}]);
	},
	
	updateStatus: function(data) {
		if(!data.id) {
			if(FMP.playingNow.lastIndex != null) {
				FMP.playingNow.lastIndex = null;
				FMP.database.updateOption('lastIndex', null);
			}
			FMP.status = {};
			return;
		}
		
		if(FMP.playingNow.lastIndex != data.index) {
			FMP.playingNow.lastIndex = data.index;
			FMP.database.updateOption('lastIndex', data.index);
		}
		
		FMP.status = data;
	},
	
	nextSong: function(data) {
		FMP.database.query([{query: FMP.squel.select().from('library').where('id', data.id), callback: function(res) {
			var title;
			if(res.result.length > 0) {
				title = res.result[0].artist +' - '+res.result[0].title;
			} else {
				title = data.title;
			}
			
			if(FMP.data.options.get('showPopupOnNewSong')) {
				FMP.popup.show(title);
			}
		}}]);
	}
};

window.addEventListener("message", function(d) 
{
	var action = d.data.action;
	var data = d.data.data;
	
	if(Receiver[action]) 
		Receiver[action](data);
	else 
		console.log(action);
	
}, false);